package com.redrose.rxandroiddemo.main.adapter;

import android.view.View;

import com.redrose.rxandroiddemo.R;
import com.redrose.rxandroiddemo.base.BaseAdapter;
import com.redrose.rxandroiddemo.bean.Chapter;
import com.redrose.rxandroiddemo.databinding.ChapterItemLayoutBinding;
import com.redrose.rxandroiddemo.main.viewmodel.MainViewModel;

import java.util.List;

public class ChapterAdapter extends BaseAdapter<ChapterItemLayoutBinding> {
    private final MainViewModel mViewModel;
    private List<Chapter> mList;

    public ChapterAdapter(List<Chapter> mList, MainViewModel viewModel) {
        this.mList = mList;
        this.mViewModel = viewModel;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.chapter_item_layout;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    protected void bindView(ChapterItemLayoutBinding binding, int position) {
        Chapter chapter = mList.get(position);
        binding.setName(chapter.getName());
        binding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mViewModel != null) {
                    mViewModel.onItemClick(chapter);
                }
            }
        });
    }
}
